package my.menu.menuImp;

import my.dao.daoImpl.BookDao;
import my.dao.daoImpl.BookTypeDao;
import my.support.BookViewer;
import my.menu.Menu;
import my.vo.voImpl.Book;
import my.vo.voImpl.BookType;
import my.vo.voImpl.User;

import java.util.List;

/**
 * @author 星环
 * @date 2022/11/1 19:21:05
 * @description 相同的部分见 {@link my.menu.menuImp.AdminChildMenus.AdminBookMenu}
 */
public class ReaderMainMenu extends Menu {
    BookDao bookDao = BookDao.getInstance();

    BookViewer bookViewer;

    public ReaderMainMenu(User user){
        //向Menu类注册选项名称、顺序、对应的方法名
        super(List.of("显示所有图书","查看某类图书","借书","还书","显示我的书"),
                List.of("showAllBooks","showBookByTypeName","borrowBook","returnBook","showMyBooks"));
        setUser(user);
        bookViewer = new BookViewer(user);
        //如果bookViewer在setUser前创建，即放在构造函数前创建，就无法获取user
    }

    /**
     * 根据uid显示自己的书
     * @return 给menu用的，每个要显示在菜单的方法都要告诉菜单调用结束后是否结束循环
     */
    public boolean showMyBooks(){
        //先根据uid获得他借的书
        List<Book> books = bookDao.getBooksByBorrowerId(user.getId());
        //再用bookViewer输出
        bookViewer.formatAndShowBooks(books);
        return true;
    }

    /**
     * 显示所有的书
     * @return
     */
    public boolean showAllBooks(){
        //直接用bookViewer
        bookViewer.showAllBooks();
        return true;
    }

    /**
     * 输入类别名，显示一类图书
     * @return
     */
    public boolean showBookByTypeName(){
        System.out.println("请选择一个类别");
        //显示所有类别
        bookViewer.showTypeOptionList();
        int typeid = sc.nextInt();

        //根据btid获得类别
        BookType bookType = BookTypeDao.getInstance().getById(typeid);


        if(bookType == null){
            System.out.println("没有这个类别");
            return true;
        }
        //根据类别获得书
        List<Book> books = bookDao.getBooksByTypeId(typeid);
        //格式化输出
        bookViewer.formatAndShowBooks(books);
        return true;
    }

    /**
     * 借书，借的书不能是已经借出的
     * @return
     */
    public boolean borrowBook(){
        System.out.println("请输入要借的书的id");
        int id = sc.nextInt();


        List<Book> books = bookDao.borrowBook(id, user.getId());
        if (books.size() != 0) {
            bookViewer.formatAndShowOneBook(books.get(0));
            System.out.println("成功借书");
        }else {
            System.out.println("查无此书或者已经被人借走了");
        }
        return true;
    }

    /**
     * 还书，只能还自己借的书
     * @return
     */
    public boolean returnBook(){
        System.out.println("您目前借的书如下");
        //  先看看自己借了哪些书
        List<Book> myBooks = bookDao.getBooksByBorrowerId(user.getId());
        bookViewer.formatAndShowBooks(myBooks);

        System.out.println("请输入您要还的书的id，输入0还了所有的书");
        int id = sc.nextInt();

        List<Book> bookList;
        //还书，
        if(id == 0){
            bookList = bookDao.returnAllBookByUserId(user.getId());
        }else {
            bookList = bookDao.returnMyBookById(user.getId(), id);
        }
        //看看还了哪些书
        bookViewer.formatAndShowBooks(bookList);
        return true;
    }
}
